home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17594 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  87 lines

  1. Path: news.spb.su!demos!usenet
  2. From: Alexey Ruzin <00alex@demos.su>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Constructor and enum Question...
  5. Date: Tue, 16 Apr 1996 21:02:58 +0400
  6. Organization: Demos Online Service
  7. Message-ID: <3173D2C2.4D68@demos.su>
  8. References: <4kujav$892@news.nde.state.ne.us>
  9. NNTP-Posting-Host: 00alex@dbs.demos.su
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
  14.  
  15. cwu wrote:
  16. > Recently I am studying C++ and have a difficulty to understand
  17. > "constructor" and "enum".  I am very appreciated any comments.
  18.  
  19. Hi, excuse me for my Engilsh.
  20.  
  21. As for 'enum':
  22.  
  23. In short, it looks like set of defines, which declare 'int' constants.
  24. These defines are grouped in type, but you don't need use it mnemonic
  25. names always. Example:
  26.  
  27. ...
  28. enum {a,b,c} f;
  29. ...
  30.  
  31. this is equal:
  32.  
  33. ...
  34. #define a 0
  35. #define b 1
  36. #define c 2
  37. int f; /* f may be 0,1,2; Hmm... i never tried to initialize with
  38.           another values... It's interesting... ??? */
  39. ...
  40.  
  41. Of course, if it was no difference between 'enum' and 'defines' there
  42. would no 'enums' and 'defines' together. But at the beginning of
  43. undestanding you may believe it.
  44.  
  45. As for 'constructor':
  46.  
  47. Again in short, It is function first called when object is initialized.
  48. In practice it is function is served to DO THIS JOB (initialization).
  49. Imagine a child, each child has a child and he knows her
  50. with birth. Lets make two classes 'Child' & 'Mother':
  51.  
  52. class Mother
  53. {
  54. ...
  55. };
  56.  
  57. class Child
  58. {
  59.   Mother mom;
  60. ...
  61. public:
  62.   Child( Mother the_mom ) { mom = the_mom; }
  63. };
  64.  
  65. ...
  66.  
  67. main()
  68. {
  69. ...
  70.   Mother a_mother;
  71.   Child a_child( a_mother );
  72. ...
  73. }
  74.  
  75. As we see, each child at moment of birth is initialized with value
  76. of his monther. Who does this work -> 'constructor'. Another way
  77. call some functions after birth to say what is mother of a child,
  78. but constructor works perfectly. In addition, there are some
  79. cases where no way to misuse (don't use) constructor (when
  80. you use references: int &a=b; :this is one way to initialize variable
  81. -> use constructor).
  82.  
  83. I hope it helps... Good luck.
  84.  
  85. to
  86.